package com.adb.droid.uc;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ListView;

/**
 * custom list view this will help to hide and seek the bottom Quick menu
 * 
 * @author bhavdip
 * 
 */
public class QuickReturnListView extends ListView {

	private boolean mscrollIsComputed = false;
	private int mItemOffsetY[];
	private int mItemCount;
	private int mHight;

	public QuickReturnListView(Context context) {
		super(context);
	}

	public QuickReturnListView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	/**
	 * Return total height of list view
	 * 
	 * @return
	 */
	public int getListHight() {
		return mHight;
	}

	/**
	 * 
	 */
	public void computeScrollY() {
		mHight = 0;
		mItemCount = getAdapter().getCount();
		if (mItemOffsetY == null) {
			mItemOffsetY = new int[mItemCount];
		}
		for (int i = 0; i < mItemCount; i++) {
			View view = getAdapter().getView(i, null, this);
			view.measure(
					MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
					MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
			mItemOffsetY[i] = mHight;
			mHight += view.getMeasuredHeight();
		}
		mscrollIsComputed = true;
	}

	public boolean scrollYIsComputed() {
		return mscrollIsComputed;
	}

	public int getComputedScrollY() {
		int pos, nScrollY, nItemY;
		View view = null;
		// current first visible item in list view might be change once we
		// scroll
		pos = getFirstVisiblePosition();
		view = getChildAt(0);
		// always first visible item top position
		nItemY = view.getTop();
		// current first visible item top - always first visible item in list
		// top
		nScrollY = mItemOffsetY[pos] - nItemY;
		return nScrollY;
	}

}
